require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/multistage'
require 'mina/puma'
# require 'mina_sidekiq/tasks'
# require 'mina/rbenv'  # for rbenv support. (https://rbenv.org)
require 'mina/rvm' # for rvm support. (https://rvm.io)

set :rvm_use_path, '/usr/local/rvm/scripts/rvm'

# shared dirs and files will be symlinked into the app-folder by the 'deploy:link_shared_paths' step.
# set :shared_dirs, fetch(:shared_dirs, []).push('somedir')
# set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml')

set :shared_dirs, %W(public/uploads tmp/pids tmp/sockets vendor/bundle)
# set :shared_files, %W(config/database.yml config/secrets.yml log config/puma.rb config/settings/production.local.yml)
set :shared_files, %W(config/database.yml config/secrets.yml config/puma.rb)

# This task is the environment that is loaded for all remote run commands, such as
# `mina deploy` or `mina rake`.
task :environment do
  # If you're using rbenv, use this to load the rbenv environment.
  # Be sure to commit your .ruby-version or .rbenv-version to your repository.
  # invoke :'rbenv:load'

  # For those using RVM, use this to load an RVM version@gemset.
  # invoke :'rvm:use', 'ruby-1.9.3-p125@default'
  invoke :'rvm:use', 'ruby-2.3.1'
end

# Put any custom commands you need to run at setup
# All paths in `shared_dirs` and `shared_paths` will be created on their own.
task :setup do
  # sidekiq needs a place to store its pid file and log file
  # command %(mkdir -p "#{fetch(:deploy_to)}/shared/pids/")
  # command %(mkdir -p "#{fetch(:deploy_to)}/shared/log/")
  # 在服务器项目目录的shared中创建log文件夹
  command %[mkdir -p "#{fetch(:shared_path)}/log"]
  command %[chmod g+rx,u+rwx "#{fetch(:shared_path)}/log"]

  command %[mkdir -p "#{fetch(:shared_path)}/tmp/pids"]
  command %[mkdir -p "#{fetch(:shared_path)}/tmp/sockets"]

  # 在服务器项目目录的shared中创建config文件夹 下同
  command %[mkdir -p "#{fetch(:shared_path)}/config"]
  command %[mkdir -p "#{fetch(:shared_path)}/vendor/bundle"]
  command %[mkdir -p "#{fetch(:shared_path)}/config/settings"]
  command %[chmod g+rx,u+rwx "#{fetch(:shared_path)}/config"]
  command %[chmod g+rx,u+rwx "#{fetch(:shared_path)}/config/settings"]

  command %[mkdir -p "#{fetch(:shared_path)}/public/uploads"]
  command %[mkdir -p "#{fetch(:shared_path)}/public/uploads/excel"]
  command %[mkdir -p "#{fetch(:shared_path)}/public/uploads/tmp"]

  command %[touch "#{fetch(:shared_path)}/config/database.yml"]
  command %[touch "#{fetch(:shared_path)}/config/puma.rb"]
  command %[touch "#{fetch(:shared_path)}/config/bluepill.rb"]
  command %[touch "#{fetch(:shared_path)}/config/secrets.yml"]
  # command %[touch "#{fetch(:shared_path)}/config/settings/production.local.yml"]

  command %[echo "-----> Be sure to edit '#{fetch(:shared_path)}/config/database.yml'."]
end

desc "Deploys the current version to the server."
task :deploy do
  # uncomment this line to make sure you pushed your local branch to the remote origin
  # invoke :'git:ensure_pushed'
  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.
    # invoke :'sidekiq:quiet'
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'

    on :launch do
      invoke :'puma:phased_restart'
      # invoke :'sidekiq:restart'
      # in_path(fetch(:current_path)) do
      #   command %{mkdir -p tmp/}
      #   command %{touch tmp/restart.txt}
      # end
    end
  end

  # you can use `run :local` to run tasks on local machine before of after the deploy scripts
  # run(:local){ say 'done' }
end

# For help in making your deploy script, see the Mina documentation:
#
#  - https://github.com/mina-deploy/mina/tree/master/docs
